﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Controls.Maps.Core;
using System.Device.Location;
using G.Navigate;

namespace G.Reader.View.Map
{
    public partial class MapPage : GBasePage
    {
        /// <summary>
        /// 偏差距离
        /// </summary>
        private readonly Point OffsetPoint = new Point(-0.00402879552734703, 0.00209359227262951);
        public MapPage()
        {
            InitializeComponent();
            UriBuilder tileSourceUri = new UriBuilder("http://r2.tiles.ditu.live.com/tiles/r{quadkey}.png?g=41");
            //MapTileLayer tileLayer = new MapTileLayer(); //初始化一个图层
            LocationRectTileSource tileSource = new LocationRectTileSource(
            tileSourceUri.Uri.ToString(),
            null,
            new Range<double>(1, 17.8));  //初始化LocationRectTileSource对象，设定显示范围及放大级别
            myMapLayer.TileSources.Add(tileSource); //指定图层的TileSource
            this.myMap.Mode = new MercatorMode();
        }
        System.Device.Location.GeoCoordinateWatcher wa;
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (wa == null)
            {
                this.myMap.Mode.ZoomLevel = GloabalData.Instance.Settings.MapSettings.ZoomLevel;
                GeoCoordinate geo = new GeoCoordinate(GloabalData.Instance.Settings.MapSettings.LastCenterLatitude, GloabalData.Instance.Settings.MapSettings.LastCenterLongitude);
                myMap.Center = GeoOffsetFixed(geo);
                wa = new GeoCoordinateWatcher();
                wa.PositionChanged += new EventHandler<System.Device.Location.GeoPositionChangedEventArgs<System.Device.Location.GeoCoordinate>>(wa_PositionChanged);
                wa.StatusChanged += new EventHandler<System.Device.Location.GeoPositionStatusChangedEventArgs>(wa_StatusChanged);
            }
            this.RegisterLoading("正在启动GPS...");
            wa.Start();
            //var stream = App.GetResourceStream(new Uri("/G.Reader;component/map.html", UriKind.Relative)).Stream;
            //using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
            //{
            //    web.NavigateToString(reader.ReadToEnd());
            //}


            //client.SearchAsync(new Reader.Map.SearchService.SearchRequest(){ 
        }

        void wa_StatusChanged(object sender, System.Device.Location.GeoPositionStatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case GeoPositionStatus.Disabled:
                    this.ShowState("请在手机的设置中启用GPS服务");
                    this.UnregisterLoading();
                    break;
                case GeoPositionStatus.NoData:
                    this.ShowState("GPS获取数据失败");
                    this.UnregisterLoading();
                    break;
                case GeoPositionStatus.Ready:
                    myMap.Center = GeoOffsetFixed(wa.Position.Location);
                    this.UnregisterLoading();
                    break;
            }

            //e.Status= GeoPositionStatus.
        }

        void wa_PositionChanged(object sender, System.Device.Location.GeoPositionChangedEventArgs<System.Device.Location.GeoCoordinate> e)
        {
            this.myLocationPushpin.Location = GeoOffsetFixed(e.Position.Location);
            this.myLocationPushpin.Visibility = System.Windows.Visibility.Visible;
           
        }

        /// <summary>
        /// 坐标偏差修正
        /// </summary>
        /// <returns></returns>
        private GeoCoordinate GeoOffsetFixed(GeoCoordinate geo)
        {
            geo.Longitude -= OffsetPoint.X;
            geo.Latitude -= OffsetPoint.Y;
            return geo;
        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            GloabalData.Instance.Settings.MapSettings.LastCenterLatitude = this.myMap.Center.Latitude;
            GloabalData.Instance.Settings.MapSettings.LastCenterLongitude = this.myMap.Center.Longitude;
            GloabalData.Instance.Settings.MapSettings.ZoomLevel = this.myMap.Mode.ZoomLevel;
            wa.Stop();
        }
    }
}